﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lextm.SharpSnmpLib;
using Lextm.SharpSnmpLib.Messaging;
using SManage.LibExtension;
using System.Net;

namespace SManage.TraceEngine
{
    internal static class EngineExtension
    {
        public static IList<Variable> WalkFromSyncItem(this ObjectIdentifier oid, SyncItem item) {
            var variables = new List<Variable>();
            Int32 retry = SyncEngineConfig.Default.RetryTimes;
            Int32 timeout = SyncEngineConfig.Default.Timeout;
            for (Int32 i = 0; i < retry; i++) {
                try {
                    Messenger.Walk(item.Version,
                            item.EndPoint,
                            item.Community,
                            oid,
                            variables,
                            timeout,
                            WalkMode.WithinSubtree);
                } catch {
                    variables = null;
                }
                if (variables != null) {
                    break;
                }
            }
            return variables;
        }

        public static IList<Variable> GetFromSyncItem(this ObjectIdentifier oid, SyncItem item) {
            IList<Variable> variables = new List<Variable> { new Variable(oid) };
            IList<Variable> result = new List<Variable>();
            Int32 retry = SyncEngineConfig.Default.RetryTimes;
            Int32 timeout = SyncEngineConfig.Default.Timeout;
            for (int i = 0; i < retry; i++) {
                try {
                    result = Messenger.Get(item.Version,
                        item.EndPoint,
                        item.Community,
                        variables,
                        timeout);
                } catch {
                    result = null;
                }
                if (result != null) {
                    break;
                }
            }
            return result;
        }
    }
}
